package com.shenhaofeng.demo.activity;

import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.widget.TextView;

import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.device.ConnectChangeSource;
import com.shenhaofeng.app.utils.net.NetUtil;


/**
 * 检查网络状态的界面
 * Created by Administrator on 2016/10/11.
 */
public class ConnectChangeActivity extends BaseActivity implements ConnectChangeSource.NetworkInfoListener {

    private ConnectChangeSource connectChangeSource;

    private TextView textView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        setContentView(textView);
        textView.setGravity(Gravity.CENTER);
        connectChangeSource = ConnectChangeSource.createInstance(this);
        connectChangeSource.registerNetworkInfoListener(this);
    }


    @Override
    protected void onDestroy() {
        connectChangeSource.unregisterNetworkInfoListener(this);
        connectChangeSource.onDestroy();
        super.onDestroy();
    }


    @Override
    public void onNewNetWorkInfo(NetworkInfo networkInfo) {
        String type = NetUtil.getConnectTypeName(this);
        boolean connected = NetUtil.isConnected(this);
        textView.setText(String.format("状态:%s,类型:%s", connected, type));

    }
}
